package lp2.projeto.agenda;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import lp2.projeto.gerenciador.FormaDeContato;

public class CadastroFormaDeContatoJDialog extends JDialog{
	
	private static final long serialVersionUID = -6682442808612566850L;
	
	// Referencias a caixa de dialogo de cadastro de formas de contatos
	private JDialog cadastroFormaDeContatoJDialog;
	protected JButton cadastroFormaDeContatoJButton;
	protected JButton fecharJButton;
	protected JTextArea comentarioFDCJTextArea;
	protected JTextField nomeFDCJTextField;
	protected JTextField enderecoFDCJTextField;
	protected CadastroContatoJDialog cadastroContatoJDialog;
	
	public CadastroFormaDeContatoJDialog(CadastroContatoJDialog cadastroContato){
		super(cadastroContato.cadastroContatoJDialog,true);
		cadastroFormaDeContatoJDialog = this;
		cadastroFormaDeContatoJDialog.setSize(400, 300);
		JPanel cadastroFormaDeContatoJPanel = (JPanel) cadastroFormaDeContatoJDialog.getContentPane();
		cadastroContatoJDialog =  cadastroContato;
		cadastroFormaDeContatoJPanel.setLayout(new GridBagLayout());
		
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.anchor = GridBagConstraints.CENTER;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.insets = new Insets(5,5,5,5); 
		
		// Criacao dos JButtons
		cadastroFormaDeContatoJButton = new JButton("Cadastrar Nova Forma de Contato");
		fecharJButton = new JButton("Fechar");
		
		
		cadastroFormaDeContatoJButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				cadastroContatoJDialog.formasDeContatoListModel.addElement(new FormaDeContato(
						nomeFDCJTextField.getText(),
						enderecoFDCJTextField.getText(),
						comentarioFDCJTextArea.getText() )
				);
				cadastroFormaDeContatoJDialog.dispose();
			}
		});
		
		
		
		fecharJButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				cadastroFormaDeContatoJDialog.dispose();
			}
		});
		
		// Criacao dos JLabels
		JLabel nomeJLabel = new JLabel("Nome:");
		JLabel enderecoJLabel = new JLabel("Endereco:");
		JLabel comentarioJLabel = new JLabel("                                           " +
				"  -   Comentario    -   ");  // TODO Consertar Isso
		
		
		// Criacao do JTextArea
		comentarioFDCJTextArea = new JTextArea(10,25);

		
		// Criacao dos JTextFields
		nomeFDCJTextField = new JTextField();
		enderecoFDCJTextField = new JTextField();
		
		// Criacao do JPanel de Comentarios
		JPanel comentarioJPanel = new JPanel();
		comentarioJPanel.setLayout(new GridBagLayout());

		constraints.weightx = 0.6;
		constraints.weighty = 0.1;
		constraints.gridx = 0;   
		constraints.gridy = 0; 
		constraints.gridwidth = 3; 
		constraints.gridheight = 1;
		comentarioJPanel.add(comentarioJLabel,constraints);
		
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weightx = 0.6;
		constraints.weighty = 0.6;
		constraints.gridx = 0;   
		constraints.gridy = 1; 
		constraints.gridwidth = 3; 
		constraints.gridheight = 2;
		comentarioJPanel.add(comentarioFDCJTextArea,constraints);
		
		// Adicao no JPanel Principal
		constraints.anchor = GridBagConstraints.CENTER;
		constraints.fill = GridBagConstraints.HORIZONTAL;

		constraints.weightx = 0.1;
		constraints.weighty = 0.1;
		constraints.insets = new Insets(5,5,5,5); 
		constraints.gridx = 0;   
		constraints.gridy = 0; 
		constraints.gridwidth = 1; 
		constraints.gridheight = 1;
		cadastroFormaDeContatoJPanel.add(nomeJLabel,constraints);
			
		constraints.weightx = 0.8;
		constraints.gridx = 1;   
		constraints.gridy = 0; 
		constraints.gridwidth = 2; 
		cadastroFormaDeContatoJPanel.add(nomeFDCJTextField,constraints);
		
		constraints.weightx = 0.1;
		constraints.gridx = 0;   
		constraints.gridy = 1; 
		constraints.gridwidth = 1; 
		cadastroFormaDeContatoJPanel.add(enderecoJLabel,constraints);
		
		constraints.weightx = 0.8;
		constraints.gridx = 1;   
		constraints.gridy = 1; 
		constraints.gridwidth = 2; 
		cadastroFormaDeContatoJPanel.add(enderecoFDCJTextField,constraints);
		
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weighty = 0.6;
		constraints.weightx = 0.8;
		constraints.gridx = 0;   
		constraints.gridy = 2; 
		constraints.gridwidth = 3; 
		constraints.gridheight = 3; 
		cadastroFormaDeContatoJPanel.add(comentarioJPanel,constraints);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weighty = 0.1;
		constraints.weightx = 0.8;
		constraints.gridx = 0;   
		constraints.gridy = 5; 
		constraints.gridwidth = 1; 
		constraints.gridheight = 1; 
		cadastroFormaDeContatoJPanel.add(fecharJButton,constraints);
		
		constraints.weighty = 0.1;
		constraints.weightx = 0.8;
		constraints.gridx = 1;   
		constraints.gridy = 5; 
		constraints.gridwidth = 2; 
		cadastroFormaDeContatoJPanel.add(cadastroFormaDeContatoJButton,constraints);
								
		
	}

}
